 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=10 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:32 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Implementing compareTo
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='comparable,compare,equals,search,sort,sorting,compareTo,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Implementing compareTo</div>

<div class='main-body'>
 
<br>The <tt>compareTo</tt> method is the sole member of the <tt><a href="http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html">Comparable</a></tt>
interface, and is not a member of <tt>Object</tt>. However, it is quite
similar in nature to <tt>equals</tt> and <tt>hashCode</tt>. It provides
a means of fully ordering objects.
<p>Implementing <tt>Comparable</tt> allows
<ul>
<li>
calling <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Collections.html">Collections</a>.sort</tt>
and <tt>Collections.binarySearch</tt></li>

<li>
calling <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html">Arrays</a>.sort</tt>
and <tt>Arrays.binarySearch</tt></li>

<li>
using objects as keys in a <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/TreeMap.html">TreeMap</a></tt></li>

<li>
using objects as elements in a <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/TreeSet.html">TreeSet</a></tt></li>
</ul>
The <tt>compareTo</tt> method needs to satisfy the following conditions.
These conditions have the goal of allowing objects to be fully sorted,
much like the sorting of a database result set on all fields.
<ul>
<li>
anticommutation :&nbsp; <tt>x.compareTo(y)</tt> is the opposite sign of
<tt>y.compareTo(x)</tt></li>

<li>
exception symmetry : <tt>x.compareTo(y)</tt> throws exactly the same exceptions
as <tt>y.compareTo(x)</tt></li>

<li>
transitivity :&nbsp; <tt>if x.compareTo(y)>0</tt> and <tt>y.compareTo(z)>0</tt>,
then <tt>x.compareTo(z)>0</tt>&nbsp; (and same for less than)</li>

<li>
&nbsp;if <tt>x.compareTo(y)==0</tt>, then <tt>x.compareTo(z)</tt>
has the same sign as <tt>y.compareTo(z)</tt></li>

<li>
consistency with <tt>equals</tt> is highly recommended, but not required
:<tt> x.compareTo(y)==0</tt>, if and only if <tt>x.equals(y)</tt> ; consistency
with <tt>equals</tt> is required for ensuring sorted collections (such
as <tt>TreeSet</tt>) are well-behaved.</li>
</ul>
One can greatly increase the performance of <tt>compareTo</tt> by comparing
first on items which are most likely to differ.
<p>When a class extends a <i>concrete</i> <tt>Comparable</tt> class and
adds a significant field, a correct implementation of <tt>compareTo</tt>
cannot be constructed. The only alternative is to use composition instead
of inheritance. (A similar situation holds true for <tt>equals</tt>. See
<i><a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=nosim/javapractices-20">Effective
Java</a></i> for more information.)
<p>Compare the various types of fields as follows :
<ul>
<li>
numeric primitive : use <tt>&lt;</tt> and <tt>></tt>. There is an exception to this rule:  
<tt>float</tt> and <tt>double</tt> primitives should be compared using 
Float.<a href='http://java.sun.com/javase/6/docs/api/java/lang/Float.html#compare(float,%20float)'>compare(float, float)</a> and 
Double.<a href='http://java.sun.com/javase/6/docs/api/java/lang/Double.html#compare(double,%20double)'>compare(double, double)</a>. 
This avoids problems associated with special border values. (Thanks to Roger Orr in the UK for pointing this out.)
</li>
<li>
<tt>boolean</tt> primitive :&nbsp; use tests of the form <tt>(x &amp;&amp;
!y)</tt></li>

<li>
<tt>Object</tt> : use <tt>compareTo</tt>. (Note that possibly-null
fields present a problem : while <tt>x.equals(null)</tt> returns
<tt>false</tt>,
<tt>x.compareTo(null)</tt>
will always throw a <tt>NullPointerException</tt>)</li>

<li>
type-safe enumeration : use <tt>compareTo</tt>, like any <tt>Object</tt></li>

<li>
collection or array : <tt>Comparable</tt> does not seem to be intended
for these kinds of fields. For example, <tt>List</tt>, <tt>Map</tt> and
<tt>Set</tt>
do not implement <tt>Comparable</tt>. As well, some collections have no
definite order of iteration, so doing an element-by-element comparison
cannot be meaningful in those cases.</li>
</ul>
If the task is to perform a sort of items which are stored in a relational
database, then it is usually much preferred to let the database perform
the sort using the ORDER BY clause, rather than in code.
<p>An alternative to implementing <tt>Comparable</tt> is passing <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Comparator.html">Comparator</a></tt>
objects as parameters. Be aware that if a <tt>Comparator</tt> compares
only one of several significant fields, then the <tt>Comparator</tt> is
very likely not synchronized with <tt>equals</tt>.
<p>All primitive wrapper classes implement <tt>Comparable</tt>. Note that
<tt><a href="http://java.sun.com/javase/6/docs/api/java/lang/Boolean.html#compareTo(java.lang.Boolean)">Boolean</a></tt>
did not implement <tt>Comparable</tt> until version 1.5, however.

<p><b>Example</b>
<br>
<PRE>

<span class='keyword'>import</span> java.util.*;
<span class='keyword'>import</span> java.io.*;

<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> Account <span class='keyword'>implements</span> Comparable&lt;Account&gt; {
  
  <span class='keyword'>enum</span> AccountType {CASH, MARGIN, RRSP};

   <span class='keyword'>public</span> Account (
      String aFirstName,
      String aLastName,
      <span class='keyword'>int</span> aAccountNumber,
      <span class='keyword'>int</span> aBalance,
      <span class='keyword'>boolean</span> aIsNewAccount,
      AccountType aAccountType
  ) {
      <span class='comment'>//..parameter validations elided
</span>      fFirstName = aFirstName;
      fLastName = aLastName;
      fAccountNumber = aAccountNumber;
      fBalance = aBalance;
      fIsNewAccount = aIsNewAccount;
      fAccountType = aAccountType;
   }

  <span class='comment'>/**
  * @param aThat is a non-null Account.
  *
  * @throws NullPointerException if aThat is null.
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>int</span> compareTo( Account aThat ) {
    <span class='keyword'>final</span> <span class='keyword'>int</span> BEFORE = -<span class='literal'>1</span>;
    <span class='keyword'>final</span> <span class='keyword'>int</span> EQUAL = <span class='literal'>0</span>;
    <span class='keyword'>final</span> <span class='keyword'>int</span> AFTER = <span class='literal'>1</span>;

    <span class='comment'>//this optimization is usually worthwhile, and can
</span>    <span class='comment'>//always be added
</span>    <span class='keyword'>if</span> ( <span class='keyword'>this</span> == aThat ) <span class='keyword'>return</span> EQUAL;

    <span class='comment'>//primitive numbers follow this form
</span>    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fAccountNumber &lt; aThat.fAccountNumber) <span class='keyword'>return</span> BEFORE;
    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fAccountNumber &gt; aThat.fAccountNumber) <span class='keyword'>return</span> AFTER;

    <span class='comment'>//booleans follow this form
</span>    <span class='keyword'>if</span> (!<span class='keyword'>this</span>.fIsNewAccount &amp;&amp; aThat.fIsNewAccount) <span class='keyword'>return</span> BEFORE;
    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fIsNewAccount &amp;&amp; !aThat.fIsNewAccount) <span class='keyword'>return</span> AFTER;

    <span class='comment'>//objects, including type-safe enums, follow this form
</span>    <span class='comment'>//note that null objects will throw an exception here
</span>    <span class='keyword'>int</span> comparison = <span class='keyword'>this</span>.fAccountType.compareTo(aThat.fAccountType);
    <span class='keyword'>if</span> ( comparison != EQUAL ) <span class='keyword'>return</span> comparison;

    comparison = <span class='keyword'>this</span>.fLastName.compareTo(aThat.fLastName);
    <span class='keyword'>if</span> ( comparison != EQUAL ) <span class='keyword'>return</span> comparison;

    comparison = <span class='keyword'>this</span>.fFirstName.compareTo(aThat.fFirstName);
    <span class='keyword'>if</span> ( comparison != EQUAL ) <span class='keyword'>return</span> comparison;

    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fBalance &lt; aThat.fBalance) <span class='keyword'>return</span> BEFORE;
    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fBalance &gt; aThat.fBalance) <span class='keyword'>return</span> AFTER;

    <span class='comment'>//all comparisons have yielded equality
</span>    <span class='comment'>//verify that compareTo is consistent with equals (optional)
</span>    assert <span class='keyword'>this</span>.equals(aThat) : <span class='literal'>"compareTo inconsistent with equals."</span>;

    <span class='keyword'>return</span> EQUAL;
  }

   <span class='comment'>/**
   * Define equality of state.
   */</span>
   <span class='keyword'>@Override</span> <span class='keyword'>public</span> <span class='keyword'>boolean</span> equals( Object aThat ) {
     <span class='keyword'>if</span> ( <span class='keyword'>this</span> == aThat ) <span class='keyword'>return</span> <span class='keyword'>true</span>;
     <span class='keyword'>if</span> ( !(aThat <span class='keyword'>instanceof</span> Account) ) <span class='keyword'>return</span> <span class='keyword'>false</span>;

     Account that = (Account)aThat;
     <span class='keyword'>return</span>
       ( <span class='keyword'>this</span>.fAccountNumber == that.fAccountNumber ) &amp;&amp;
       ( <span class='keyword'>this</span>.fAccountType == that.fAccountType ) &amp;&amp;
       ( <span class='keyword'>this</span>.fBalance == that.fBalance ) &amp;&amp;
       ( <span class='keyword'>this</span>.fIsNewAccount == that.fIsNewAccount ) &amp;&amp;
       ( <span class='keyword'>this</span>.fFirstName.equals(that.fFirstName) ) &amp;&amp;
       ( <span class='keyword'>this</span>.fLastName.equals(that.fLastName) );
   }

   <span class='comment'>/**
   * A class that overrides equals must also override hashCode.
   */</span>
   <span class='keyword'>@Override</span> <span class='keyword'>public</span> <span class='keyword'>int</span> hashCode() {
     <span class='keyword'>int</span> result = HashCodeUtil.SEED;
     result = HashCodeUtil.hash( result, fAccountNumber );
     result = HashCodeUtil.hash( result, fAccountType );
     result = HashCodeUtil.hash( result, fBalance );
     result = HashCodeUtil.hash( result, fIsNewAccount );
     result = HashCodeUtil.hash( result, fFirstName );
     result = HashCodeUtil.hash( result, fLastName );
     <span class='keyword'>return</span> result;
   }

   <span class='comment'>////  PRIVATE  ///////
</span>
   <span class='keyword'>private</span> String fFirstName; <span class='comment'>//non-null
</span>   <span class='keyword'>private</span> String fLastName;  <span class='comment'>//non-null
</span>   <span class='keyword'>private</span> <span class='keyword'>int</span> fAccountNumber;
   <span class='keyword'>private</span> <span class='keyword'>int</span> fBalance;
   <span class='keyword'>private</span> <span class='keyword'>boolean</span> fIsNewAccount;

   <span class='comment'>/**
   * Type of the account, expressed as a type-safe enumeration (non-null).
   */</span>
   <span class='keyword'>private</span> AccountType fAccountType;

   <span class='comment'>/**
   * Exercise compareTo.
   */</span>
   <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main (String[] aArguments) {
     <span class='comment'>//Note the difference in behaviour in equals and compareTo, for nulls:
</span>     String text = <span class='literal'>"blah"</span>;
     Integer number = <span class='keyword'>new</span> Integer(<span class='literal'>10</span>);
     <span class='comment'>//x.equals(null) always returns false:
</span>     System.out.println(<span class='literal'>"false: "</span> + text.equals(<span class='keyword'>null</span>));
     System.out.println(<span class='literal'>"false: "</span> + number.equals(<span class='keyword'>null</span>) );
     <span class='comment'>//x.compareTo(null) always throws NullPointerException:
</span>     <span class='comment'>//System.out.println( text.compareTo(null) );
</span>     <span class='comment'>//System.out.println( number.compareTo(null) );
</span>
     Account flaubert = <span class='keyword'>new</span> Account(
      <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>,<span class='keyword'>true</span>, AccountType.MARGIN
     );

     <span class='comment'>//all of these other versions of "flaubert" differ from the
</span>     <span class='comment'>//original in only one field
</span>     Account flaubert2 = <span class='keyword'>new</span> Account(
       <span class='literal'>"Guy"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>, <span class='keyword'>true</span>, AccountType.MARGIN
     );
     Account flaubert3 = <span class='keyword'>new</span> Account(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"de Maupassant"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>, <span class='keyword'>true</span>, AccountType.MARGIN
     );
     Account flaubert4 = <span class='keyword'>new</span> Account(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>2004</span>, <span class='literal'>0</span>, <span class='keyword'>true</span>, AccountType.MARGIN
     );
     Account flaubert5 = <span class='keyword'>new</span> Account(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>1</span>, <span class='keyword'>true</span>, AccountType.MARGIN
     );
     Account flaubert6 = <span class='keyword'>new</span> Account(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>, <span class='keyword'>false</span>, AccountType.MARGIN
     );
     Account flaubert7 = <span class='keyword'>new</span> Account(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>, <span class='keyword'>true</span>, AccountType.CASH
     );

     System.out.println( <span class='literal'>"0: "</span> +  flaubert.compareTo(flaubert) );
     System.out.println( <span class='literal'>"first name +: "</span> +  flaubert2.compareTo(flaubert) );
     <span class='comment'>//Note capital letters precede small letters
</span>     System.out.println( <span class='literal'>"last name +: "</span> +  flaubert3.compareTo(flaubert) );
     System.out.println( <span class='literal'>"acct number +: "</span> +  flaubert4.compareTo(flaubert) );
     System.out.println( <span class='literal'>"balance +: "</span> +  flaubert5.compareTo(flaubert) );
     System.out.println( <span class='literal'>"is new -: "</span> +  flaubert6.compareTo(flaubert) );
     System.out.println( <span class='literal'>"account type -: "</span> +  flaubert7.compareTo(flaubert) );
   }
} 
</PRE>
<br>
<br>A sample run of this class gives:
<br><tt>>java -cp . Account</tt>
<br><tt>false: false</tt>
<br><tt>false: false</tt>
<br><tt>0: 0</tt>
<br><tt>first name +: 6</tt>
<br><tt>last name +: 30</tt>
<br><tt>acct number +: 1</tt>
<br><tt>balance +: 1</tt>
<br><tt>is new -: -1</tt>
<br><tt>account type -: -1</tt>
<p>In the older JDK 1.4, there are two differences :
<ul>
<li>
the type-safe version of the&nbsp;<tt>Comparable</tt>&nbsp; interface cannot
be used. Instead,&nbsp;<tt>Object</tt> appears, along with a related cast
operation</li>

<li>
<tt>Boolean</tt> objects must be treated differently from other wrapper
classes, since&nbsp;<tt>Boolean</tt> did not implement&nbsp;<tt>Comparable</tt>
until JDK 1.5.</li>
</ul>
<b>Example</b> :
<br>
<PRE>

<span class='keyword'>import</span> java.util.*;
<span class='keyword'>import</span> java.io.*;

<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> AccountOld <span class='keyword'>implements</span> Comparable {

   <span class='keyword'>public</span> AccountOld (
      String aFirstName,
      String aLastName,
      <span class='keyword'>int</span> aAccountNumber,
      <span class='keyword'>int</span> aBalance,
      <span class='keyword'>boolean</span> aIsNewAccount,
      AccountType aAccountType
   ) {
      <span class='comment'>//..parameter validations elided
</span>      fFirstName = aFirstName;
      fLastName = aLastName;
      fAccountNumber = aAccountNumber;
      fBalance = aBalance;
      fIsNewAccount = aIsNewAccount;
      fAccountType = aAccountType;
   }

  <span class='comment'>/**
  * @param aThat is a non-null AccountOld.
  *
  * @throws NullPointerException if aThat is null.
  * @throws ClassCastException if aThat is not an AccountOld object.
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>int</span> compareTo( Object aThat ) {
    <span class='keyword'>final</span> <span class='keyword'>int</span> BEFORE = -<span class='literal'>1</span>;
    <span class='keyword'>final</span> <span class='keyword'>int</span> EQUAL = <span class='literal'>0</span>;
    <span class='keyword'>final</span> <span class='keyword'>int</span> AFTER = <span class='literal'>1</span>;

    <span class='comment'>//this optimization is usually worthwhile, and can
</span>    <span class='comment'>//always be added
</span>    <span class='keyword'>if</span> ( <span class='keyword'>this</span> == aThat ) <span class='keyword'>return</span> EQUAL;

    <span class='keyword'>final</span> AccountOld that = (AccountOld)aThat;

    <span class='comment'>//primitive numbers follow this form
</span>    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fAccountNumber &lt; that.fAccountNumber) <span class='keyword'>return</span> BEFORE;
    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fAccountNumber &gt; that.fAccountNumber) <span class='keyword'>return</span> AFTER;

    <span class='comment'>//booleans follow this form
</span>    <span class='keyword'>if</span> (!<span class='keyword'>this</span>.fIsNewAccount &amp;&amp; that.fIsNewAccount) <span class='keyword'>return</span> BEFORE;
    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fIsNewAccount &amp;&amp; !that.fIsNewAccount) <span class='keyword'>return</span> AFTER;

    <span class='comment'>//Objects, including type-safe enums, follow this form.
</span>    <span class='comment'>//Exception : Boolean implements Comparable in JDK 1.5, but not in 1.4
</span>    <span class='comment'>//Note that null objects will throw an exception here.
</span>    <span class='keyword'>int</span> comparison = <span class='keyword'>this</span>.fAccountType.compareTo(that.fAccountType);
    <span class='keyword'>if</span> ( comparison != EQUAL ) <span class='keyword'>return</span> comparison;

    comparison = <span class='keyword'>this</span>.fLastName.compareTo(that.fLastName);
    <span class='keyword'>if</span> ( comparison != EQUAL ) <span class='keyword'>return</span> comparison;

    comparison = <span class='keyword'>this</span>.fFirstName.compareTo(that.fFirstName);
    <span class='keyword'>if</span> ( comparison != EQUAL ) <span class='keyword'>return</span> comparison;

    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fBalance &lt; that.fBalance) <span class='keyword'>return</span> BEFORE;
    <span class='keyword'>if</span> (<span class='keyword'>this</span>.fBalance &gt; that.fBalance) <span class='keyword'>return</span> AFTER;

    <span class='comment'>//all comparisons have yielded equality
</span>    <span class='comment'>//verify that compareTo is consistent with equals (optional)
</span>    assert <span class='keyword'>this</span>.equals(that) : <span class='literal'>"compareTo inconsistent with equals."</span>;

    <span class='keyword'>return</span> EQUAL;
  }

   <span class='comment'>/**
   * Define equality of state.
   */</span>
   <span class='keyword'>@Override</span> <span class='keyword'>public</span> <span class='keyword'>boolean</span> equals( Object aThat ) {
     <span class='keyword'>if</span> ( <span class='keyword'>this</span> == aThat ) <span class='keyword'>return</span> <span class='keyword'>true</span>;
     <span class='keyword'>if</span> ( !(aThat <span class='keyword'>instanceof</span> Account) ) <span class='keyword'>return</span> <span class='keyword'>false</span>;

     AccountOld that = (AccountOld)aThat;
     <span class='keyword'>return</span>
       ( <span class='keyword'>this</span>.fAccountNumber == that.fAccountNumber ) &amp;&amp;
       ( <span class='keyword'>this</span>.fAccountType == that.fAccountType ) &amp;&amp;
       ( <span class='keyword'>this</span>.fBalance == that.fBalance ) &amp;&amp;
       ( <span class='keyword'>this</span>.fIsNewAccount == that.fIsNewAccount ) &amp;&amp;
       ( <span class='keyword'>this</span>.fFirstName.equals(that.fFirstName) ) &amp;&amp;
       ( <span class='keyword'>this</span>.fLastName.equals(that.fLastName) );
   }

   <span class='comment'>/**
   * A class that overrides equals must also override hashCode.
   */</span>
   <span class='keyword'>@Override</span> <span class='keyword'>public</span> <span class='keyword'>int</span> hashCode() {
     <span class='keyword'>int</span> result = HashCodeUtil.SEED;
     result = HashCodeUtil.hash( result, fAccountNumber );
     result = HashCodeUtil.hash( result, fAccountType );
     result = HashCodeUtil.hash( result, fBalance );
     result = HashCodeUtil.hash( result, fIsNewAccount );
     result = HashCodeUtil.hash( result, fFirstName );
     result = HashCodeUtil.hash( result, fLastName );
     <span class='keyword'>return</span> result;
   }

   <span class='comment'>////  PRIVATE  ///////
</span>
   <span class='keyword'>private</span> String fFirstName; <span class='comment'>//non-null
</span>   <span class='keyword'>private</span> String fLastName;  <span class='comment'>//non-null
</span>   <span class='keyword'>private</span> <span class='keyword'>int</span> fAccountNumber;
   <span class='keyword'>private</span> <span class='keyword'>int</span> fBalance;
   <span class='keyword'>private</span> <span class='keyword'>boolean</span> fIsNewAccount;

   <span class='comment'>/**
   * Type of the account, expressed as a type-safe enumeration (non-null).
   */</span>
   <span class='keyword'>private</span> AccountType fAccountType;

   <span class='comment'>/**
   * Exercise compareTo.
   */</span>
   <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main (String[] aArguments) {
     <span class='comment'>//Note the difference in behaviour in equals and compareTo, for nulls:
</span>     String text = <span class='literal'>"blah"</span>;
     Integer number = <span class='keyword'>new</span> Integer(<span class='literal'>10</span>);
     <span class='comment'>//x.equals(null) always returns false:
</span>     System.out.println(<span class='literal'>"false: "</span> + text.equals(<span class='keyword'>null</span>));
     System.out.println(<span class='literal'>"false: "</span> + number.equals(<span class='keyword'>null</span>) );
     <span class='comment'>//x.compareTo(null) always throws NullPointerException:
</span>     <span class='comment'>//System.out.println( text.compareTo(null) );
</span>     <span class='comment'>//System.out.println( number.compareTo(null) );
</span>
     AccountOld flaubert = <span class='keyword'>new</span> AccountOld(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>, <span class='keyword'>true</span>, AccountType.MARGIN
     );

     <span class='comment'>//all of these other versions of "flaubert" differ from the
</span>     <span class='comment'>//original in only one field
</span>     AccountOld flaubert2 = <span class='keyword'>new</span> AccountOld(
       <span class='literal'>"Guy"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>, <span class='keyword'>true</span>, AccountType.MARGIN
     );
     AccountOld flaubert3 = <span class='keyword'>new</span> AccountOld(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"de Maupassant"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>, <span class='keyword'>true</span>, AccountType.MARGIN
     );
     AccountOld flaubert4 = <span class='keyword'>new</span> AccountOld(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>2004</span>, <span class='literal'>0</span>, <span class='keyword'>true</span>, AccountType.MARGIN
     );
     AccountOld flaubert5 = <span class='keyword'>new</span> AccountOld(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>1</span>, <span class='keyword'>true</span>, AccountType.MARGIN
     );
     AccountOld flaubert6 = <span class='keyword'>new</span> AccountOld(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>, <span class='keyword'>false</span>, AccountType.MARGIN
     );
     AccountOld flaubert7 = <span class='keyword'>new</span> AccountOld(
       <span class='literal'>"Gustave"</span>, <span class='literal'>"Flaubert"</span>, <span class='literal'>1003</span>, <span class='literal'>0</span>, <span class='keyword'>true</span>, AccountType.CASH
     );

     System.out.println( <span class='literal'>"0: "</span> +  flaubert.compareTo(flaubert) );
     System.out.println( <span class='literal'>"first name +: "</span> +  flaubert2.compareTo(flaubert) );
     <span class='comment'>//Note capital letters precede small letters
</span>     System.out.println( <span class='literal'>"last name +: "</span> +  flaubert3.compareTo(flaubert) );
     System.out.println( <span class='literal'>"acct number +: "</span> +  flaubert4.compareTo(flaubert) );
     System.out.println( <span class='literal'>"balance +: "</span> +  flaubert5.compareTo(flaubert) );
     System.out.println( <span class='literal'>"is new -: "</span> +  flaubert6.compareTo(flaubert) );
     System.out.println( <span class='literal'>"account type -: "</span> +  flaubert7.compareTo(flaubert) );
   }
} 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction2161-2.html?Id=1'>Type-Safe Enumerations</a> <br>
 
  
  <a href='TopicAction1deb-2.html?Id=17'>Implementing equals</a> <br>
 
  
  <a href='TopicAction78db-2.html?Id=28'>Implementing hashCode</a> <br>
 
  
  <a href='TopicAction7bff-2.html?Id=106'>Do not perform database tasks in code </a> <br>
 
  
  <a href='TopicActionaa3f-2.html?Id=225'>Modernize old code</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='10'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=10 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:32 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
